//
//  ByPersonalAuthenticationVC.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/19.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByPersonalAuthenticationVC.h"

/** 辅助 */
#import "ByIconChooseDataSource.h"              // 头像选择数据源


#import "ByPersonalViewModel.h"                 // vm

@interface ByPersonalAuthenticationVC () <ByPersonalAuthenticateDelegate>

@property (nonatomic, strong) UIImageView *contentView;             // 执照图片
@property (nonatomic, strong) UIView *emptyView;                    // 未选择图片时候的视图

@property (nonatomic, strong) ByPersonalViewModel *vm;

@property (nonatomic, strong) ByIconChooseDataSource *iconChooseDataSource;

@end

@implementation ByPersonalAuthenticationVC

#pragma mark - getter
- (ByPersonalViewModel *)vm {
    if (!_vm) {
        _vm = [[ByPersonalViewModel alloc] init];
        _vm.authenticateDelegate = self;
    }
    return _vm;
}
- (UIImageView *)contentView {
    if (!_contentView) {
        _contentView = [[UIImageView alloc] init];
        _contentView.frame = self.emptyView.bounds;
        _contentView.hidden = YES;
        [self.emptyView addSubview:_contentView];
    }
    return _contentView;
}
- (ByIconChooseDataSource *)iconChooseDataSource {
    if (!_iconChooseDataSource) {
        _iconChooseDataSource = [[ByIconChooseDataSource alloc] init];
        [_iconChooseDataSource fireWithViewController:self];
    }
    return _iconChooseDataSource;
}

#pragma mark - viewcontroller method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"身份认证";
    
    [self createLeftBackBarButtonItem];
    
    [self createViews];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSLog(@"%@", BY_USER);
    if (BY_USER.is_auth_e.integerValue == 1) {  // 已认证
        self.contentView.hidden = NO;
        [self.contentView setImageWithURL:[NSURL URLWithString:BY_USER.autn_url] placeholderImage:DEFAULT_IMAGE completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
            if (!error) {
                image = [image getCenterRatilImageWithRatio:4.0/5];
            }
        }];
    }
}

#pragma mark - net
/** 上传认证图片 */
- (void)uploadAuthentication {
    [ByCoreToolAlert alertLoading:@"正在上传"];
    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
    params.iconImage = self.contentView.image;
    [self.vm authenticate:params];
}
- (void)AuthenticateSuccess:(ByNetBaseResult *)result {
    [ByCoreToolAlert alertDismiss];
    NSLog(@"%@", result.data);
    /** 更新用户信息 */
    ByNetUserModel *user = BY_USER;
    user.is_auth_e = @"1";
    user.autn_url = [NSString stringWithFormat:@"%@?id=%@&time=%lf", BOYE_PROFILE_URL, user.uid, [NSDate currentDateTimeInterval]];
    [ByCommonCache setUserInfo:user];
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)AuthenticateFailure:(ByNetBaseResult *)result {
    [ByCoreToolAlert alertError:result.msg];
}

#pragma mark - create view
- (void)createViews {
    
    /** 凭证上传视图 */
    self.emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 0.4, SCREEN_WIDTH * 0.5)];
    [self.view addSubview:self.emptyView];
    self.emptyView.center = CGPointMake(self.view.width / 2, self.view.height * 0.3);
    self.emptyView.layer.cornerRadius = 2;
    self.emptyView.layer.borderColor = [UIColor colorWithWhite:0.9 alpha:1].CGColor;
    self.emptyView.layer.borderWidth = 1;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pictureSelect)];
    [self.emptyView addGestureRecognizer:tap];      // 添加点击手势
    
    /** 十字线 */
    UILabel *line1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.emptyView.width / 2, 2)];
    line1.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    [self.emptyView addSubview:line1];
    UILabel *line2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 2, line1.width)];
    line2.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    [self.emptyView addSubview:line2];
    
    /** 文字 */
    UILabel *msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.emptyView.width, 20)];
    msgLabel.text = @"请上传营业执照";
    msgLabel.font = FONT(14);
    msgLabel.textAlignment = NSTextAlignmentCenter;
    msgLabel.textColor = [UIColor colorWithWhite:0.8 alpha:1];
    [self.emptyView addSubview:msgLabel];
    
    msgLabel.center = CGPointMake(self.emptyView.width / 2, self.emptyView.height - 30);
    line1.center = CGPointMake(self.emptyView.width / 2, msgLabel.top / 2);
    line2.center = line1.center;
    
    
    /** 保存按钮 */
    [BoyeViewHandle createButtonInView:self.view config:^(UIButton *button) {
        button.frame = CGRectMake(SCREEN_WIDTH * 0.1, 0, SCREEN_WIDTH * 0.8, 40);
        button.backgroundColor = JM_THEME_COLOR;
        button.titleLabel.font = FONT(16);
        [button setTitle:@"保存" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
        button.layer.cornerRadius = 5;
        button.bottom = SCREEN_HEIGHT - 64 - 10;
    }];
}


#pragma mark - 点击事件
/** 图片选择事件 */
- (void)pictureSelect {
    
    [self.iconChooseDataSource makeConfigToSelectIcon];
    __weak typeof(self) weakself = self;
    [self.iconChooseDataSource setCompleteBlock:^(UIImage *image) {
        
        image = [image getCenterRatilImageWithRatio:(4.0/5.0)];
        weakself.contentView.image = image;
        weakself.contentView.hidden = NO;
    }];
}

/** 保存事件 */
- (void)saveClick {
    
    /** TODO:上传请求 */
    [self uploadAuthentication];
}



#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
